package com.zhq.zhq_blog.controller;


import com.zhq.zhq_blog.entity.*;
import com.zhq.zhq_blog.entity.add.AddCommentBean;
import com.zhq.zhq_blog.entity.add.AddCommentReplyBean;
import com.zhq.zhq_blog.entity.result.CommentResultBean;
import com.zhq.zhq_blog.entity.result.ResultBean;
import com.zhq.zhq_blog.exception.BusinessException;
import com.zhq.zhq_blog.exception.CodeEnum;
import com.zhq.zhq_blog.service.ICommentService;
import com.zhq.zhq_blog.service.IReplyService;
import com.zhq.zhq_blog.service.IUserService;
import com.zhq.zhq_blog.util.CheckUtil;
import com.zhq.zhq_blog.util.JWTUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/blog/comment")
@Api(description = "评论管理")
public class CommentDetailController {

    ICommentService service;
    IReplyService replyService;
    IUserService userService;

    public CommentDetailController(ICommentService service,IReplyService replyService,IUserService userService){
        this.service = service;
        this.replyService = replyService;
        this.userService = userService;
    }

    @GetMapping("queryCommentList/{blog_id}")
    @ApiOperation(value = "查询博客所有评论", notes = "查询博客所有评论")
    public ResultBean<CommentResultBean<List<Comment>>> queryCommentListById(@PathVariable int blog_id) {
        CheckUtil.requireNotEmpty(blog_id, "blogId不能为空");
        System.out.println("queryCommentListById:执行");
        List<Comment> commentList = service.queryCommentList(blog_id);
        CommentResultBean<List<Comment>> commentResultBean = CommentResultBean.success(commentList.size(),commentList);
        if (commentList == null) {
            return ResultBean.fail(new BusinessException(CodeEnum.COMMON_FAIL_CODE, "没有评论"));
        } else {
            return ResultBean.success(commentResultBean);

        }
    }

    @GetMapping("queryComment/{comment_id}")
    @ApiOperation(value = "查询评论所有回复",notes = "查询评论所有回复")
    public ResultBean<Comment> queryComment(@PathVariable int comment_id){
        Comment comment = service.queryComment(comment_id);
        if (comment == null){
            return ResultBean.fail(new BusinessException(CodeEnum.COMMON_FAIL_CODE,"没有此条评论"));
        }else {
            return ResultBean.success(comment);
        }
    }

    @PostMapping("add_comment")
    @ApiOperation(value = "添加评论",notes = "添加评论")
    public ResultBean<Integer> addComment(@RequestBody AddCommentBean commentBean, @RequestHeader String token) {
        int userId = JWTUtils.getIdByToken(token);//这里是之前的拦截器放行的。所以一定正确
        CheckUtil.requireNotEmpty(commentBean.blogId, "blogId不能为空");
        CheckUtil.requireNotEmpty(commentBean.content, "内容不能为空");
        CheckUtil.requireNotEmpty(commentBean.imgId, "imgId不能为空");
        User user = userService.getById(userId);
        LocalDateTime localDateTime = LocalDateTime.now();

        Comment comment = new Comment();
        comment.setContent(commentBean.content);
        comment.setImgId(commentBean.imgId);
        comment.setBlogId(commentBean.blogId);
        comment.setNickName(user.getUsername());
        comment.setUserLogo(user.getIconUrl());
        comment.setReplyTotal(0);
        comment.setCreateDate(localDateTime);

        boolean save = service.save(comment);
        return ResultBean.success(comment.getId());
    }

    @PostMapping("add_comment_reply")
    @ApiOperation(value = "添加评论",notes = "添加评论")
    public ResultBean<Integer> addCommentReply(@RequestBody AddCommentReplyBean addCommentReplyBean, @RequestHeader String token) {
        int userId = JWTUtils.getIdByToken(token);//这里是之前的拦截器放行的。所以一定正确
        CheckUtil.requireNotEmpty(addCommentReplyBean.commentId, "commentId不能为空");
        CheckUtil.requireNotEmpty(addCommentReplyBean.content, "内容不能为空");
        CheckUtil.requireNotEmpty(addCommentReplyBean.status, "status不能为空");
        User user = userService.getById(userId);
        LocalDateTime localDateTime = LocalDateTime.now();

        Reply reply = new Reply();
        reply.setCommentId(addCommentReplyBean.commentId);
        reply.setContent(addCommentReplyBean.content);
        reply.setStatus(addCommentReplyBean.status);
        reply.setNickName(user.getUsername());
        reply.setCreateDate(localDateTime);

        boolean save = replyService.save(reply);
        return ResultBean.success(reply.getId());
    }

}
